<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="药品选择" prop="materialNo">
                <el-select v-model="ruleForm.materialNo" class="w100p" clearable filterable placeholder="请选择物料"
                    @change="querymaterialName">
                    <!-- 动保：4165464165465146516 -->
                    <!-- 药品：1077222993386450944 -->
                    <el-option
                        v-if="item.materialTypeNo == '1077222993386450944' || item.materialTypeNo == '4165464165465146516'"
                        v-for="item, index in materialList" :key="index" :label="item.materialName"
                        :value="item.materialNo"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="用量最大值" prop="warnValue">
                <el-input type="number" v-model="ruleForm.warnValue" @change="handleWarnValue" placeholder="请输入用药值">
                    <template slot="append" v-show="ruleForm.unit">{{ ruleForm.unit ? ruleForm.unit + '/平方米' : ''
                        }}</template>
                </el-input>
            </el-form-item>
            <!-- <el-form-item label="是否选择月份" prop="filterType">
                <el-select v-model="ruleForm.filterType" class="w100p" placeholder="请选择">
                    <el-option label="不使用月份" :value="0"></el-option>
                    <el-option label="使用月份" :value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="filterStartValue" v-if="ruleForm.filterType">
                <el-select v-model="ruleForm.filterStartValue" class="w100p" clearable filterable placeholder="请选择结束月">
                    <el-option :disabled="ruleForm.filterStartValue && (m > Number(ruleForm.filterEndValue || 0))"
                        v-for="m, i in 12" :key="i" :label="(m) + '月'" :value="m"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="filterEndValue" v-if="ruleForm.filterType">
                <el-select v-model="ruleForm.filterEndValue" class="w100p" clearable filterable placeholder="请选择开始月">
                    <el-option :disabled="ruleForm.filterEndValue && (m < Number(ruleForm.filterStartValue || 0))"
                        v-for="m, i in 12" :key="i" :label="(m) + '月'" :value="m"></el-option>
                </el-select>
            </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import medicineConfig from '@/api/medicineConfig'
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        facilityList: {
            type: Array,
            default: () => {
                return []
            }
        },
        materialList: {
            type: Array,
            default: () => {
                return []
            }
        },
        type: {
            type: String,
            default: () => {
                return ''
            }
        },
    },
    data() {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0) {
                callback()
            } else {
                callback(new Error('请输入大于0的数！'))
            }
        }
        return {
            filterConditionList: [
                {
                    name: "每年",
                    key: 'ANNUALLY'
                },
                {
                    name: "周期",
                    key: 'CYCLE'
                },
                {
                    name: "每月",
                    key: 'MONTHLY'
                },
                {
                    name: "每周",
                    key: 'WEEKLY'
                }
            ],
            dayNum: 31,
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            currentMonth: -1,
            rules: {
                warnValue: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                filterEndValue: [
                    { required: true, message: '请选择月份', trigger: 'change' }
                ],
                filterStartValue: [
                    { required: true, message: '请选择月份', trigger: 'change' }
                ],
                materialNo: [
                    { required: true, message: '请选择药品', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() {
        const date = new Date();
        this.currentMonth = date.getMonth() + 1;
        console.log(this.currentMonth)
    },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.$set(this.ruleForm, 'filterType', 0);
                this.addEditApi = medicineConfig.add;
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.addEditApi = medicineConfig.edit;
                this.addEditName = '编辑';
            }
        },
        handleWarnValue(v) {
            this.ruleForm.warnValue = Number(this.ruleForm.warnValue).toFixed(4)
        },
        querymaterialName(no) {
            let result = this.materialList.find(obj => obj.materialNo == no);
            result.materialNo && this.$set(this.ruleForm, 'materialName', result.materialName)
            result.materialNo && this.$set(this.ruleForm, 'unit', result.unitName)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let { filterEndValue, filterStartValue } = this.ruleForm
                    this.addEditApi({
                        ...this.ruleForm,
                        filterEndValue: this.ruleForm.filterType ? filterEndValue : '',
                        filterStartValue: this.ruleForm.filterType ? filterStartValue : '',
                    }).then(res => {
                        this.cancel();
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.code == 1 ? '操作成功' : res.msg || '操作失败')
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
</script>
<style lang="scss" scoped></style>